package com.iSnail.ms.gather.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;
import org.jdesktop.jdic.browser.WebBrowser;

import com.iSnail.ms.util.Config;
import com.iSnail.ms.util.Const;

public class BrowserPanel extends JPanel {
	Logger log = Logger.getLogger(BrowserPanel.class.getName());
	WebBrowser browser = null;

	public BrowserPanel() {
		super();
		setLayout(new GridBagLayout());
		add(getBrowser(),
				new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
	}

	private void setURL(String url) {
		try {
			browser.setURL(new URL(url));
		} catch (MalformedURLException e) {
			log.warn("url : " + url + " can not open.", e);
		}
	}
	
	
	
	private WebBrowser getBrowser() {
		if(browser==null){
			browser = new WebBrowser();
			browser.addMouseListener(new MouseAdapter() {
				public void keyReleased(MouseEvent e) {
					boolean isLeft = SwingUtilities.isLeftMouseButton(e);
					int times = e.getClickCount();
					if( isLeft && times==2 ){
						MainFrame.fullScreeen();
					}
				}
			});
			setURL(Config.getConfigValue(Const.INDEX_URL));
		}
		return browser;
	}
}